package uit.bus;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import uit.hcm.object.DiemDuLich;
import uit.hcm.object.JSONParser;
import uit.hcm.object.MapOverlayCustom;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;

public class LocationCurrentActivity extends MapActivity {

	private MapView _mapView;
	private MapController _mc;
	private GeoPoint _p;
	private LocationManager _lm;
	private Location _current = new Location("current");
	private MyLocationListener _locationListener;
	private JSONParser _jParser = new JSONParser();
	private List<DiemDuLich> listTravelLocation;
	private String _url = "";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.locationcurrent);

		_url = getString(R.string.link_web) + "showDestinations.php";
		listTravelLocation = new ArrayList<DiemDuLich>();
		_current.setLatitude(10.801354);
		_current.setLongitude(106.711063);

		ProgressDialog progress = new ProgressDialog(
				LocationCurrentActivity.this);
		new CustomAsyncTask(progress).execute();

		_lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		_locationListener = new MyLocationListener();
		_lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 10,
				_locationListener);

		_p = new GeoPoint((int) (_current.getLatitude() * 1E6),
				(int) (_current.getLongitude() * 1E6));

		loadMap();

	}

	private void loadMap() {

		if (_p != null) {
			_mapView = (MapView) findViewById(R.id.map_view);
			_mapView.setBuiltInZoomControls(true);
			_mc = _mapView.getController();
			_mc.animateTo(_p);
			_mc.setZoom(16);
		}

	}

	public void mapOverlay() {

		MapOverlayCustom mapOverlay = new MapOverlayCustom(_p, this,
				listTravelLocation);
		List<Overlay> listOfOverlays = _mapView.getOverlays();
		listOfOverlays.clear();
		listOfOverlays.add(mapOverlay);
		_mapView.invalidate();
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) 
	{
	    if (keyCode == KeyEvent.KEYCODE_BACK) 
	    {
	    	
	    	Intent intent = new Intent(this,MainActivity.class);
			startActivity(intent);
			finish();
	       

	    }
	    return super.onKeyDown(keyCode, event);
	}
	private class MyLocationListener implements LocationListener {

		@Override
		public void onLocationChanged(Location location) {
			// TODO Auto-generated method stub
			if (location != null) {

				_current = location;
				loadMap();
				_p = new GeoPoint((int) (_current.getLatitude() * 1E6),
						(int) (_current.getLongitude() * 1E6));
				mapOverlay();
			}
		}

		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub

		}
	}

	private class CustomAsyncTask extends AsyncTask<String, String, String> {

		public CustomAsyncTask(ProgressDialog progress) {

		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			// _progressDialog.show();

		}

		@Override
		protected String doInBackground(String... args) {
			// TODO Auto-generated method stub
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			JSONObject json = _jParser.makeHttpRequest(_url, "POST", params);
			getData(json);

			return null;
		}

		@Override
		protected void onPostExecute(String result) {

			return;

		}

		public void getData(JSONObject json) {
			listTravelLocation.clear();
			try {
				JSONArray tv = json.getJSONArray("diem_du_lich");
				for (int i = 0; i < tv.length(); i++) {
					JSONObject jsObject = tv.getJSONObject(i);
					DiemDuLich dl = new DiemDuLich();
					dl.setDiemId(jsObject.getInt("ma_diem"));
					dl.setTenDiem(jsObject.getString("ten_diem"));
					dl.setGioiThieu(jsObject.getString("gioi_thieu"));
					dl.setKinhDo(jsObject.getDouble("kinh_do"));
					dl.setViDo(jsObject.getDouble("vi_do"));
					listTravelLocation.add(dl);
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
